package my.io.hadoop.jobs.db;

import cn.hutool.db.Entity;
import cn.hutool.db.handler.HandleHelper;
import my.io.hadoop.jobs.MapToReduce;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据
 *
 * @author Xin
 * @date 2024/03/26
 */
public class DBJobRow extends MapToReduce implements DBWritable {

    @Override
    public void write(PreparedStatement ps) throws SQLException {
        int i = 1;
        for (String name : DBJob.OUTPUT_COLS) {
            Object o = this.get(name);
            if (o instanceof BigDecimal) {
                ps.setBigDecimal(i, (BigDecimal) o);
            } else {
                ps.setString(i, o.toString());
            }
            i++;
        }
    }

    @Override
    public void readFields(ResultSet resultSet) throws SQLException {
        Entity entity = HandleHelper.handleRow(resultSet);
        this.putAll(entity);
    }
}
